﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PC_Touch.DataOP
{
    public class DBParameterShape
    {
        public int id { get; set; }
        public string createDataTime { get; set; }
        public string dataName { get; set; }
        public string dataAddress { get; set; }
        public string dataType { get; set; }
        public int dataLength { get; set; }
        public string unit { get; set; }
        public string remark { get; set; }
    }
    public class DBValue
    {
        public DBParameterShape parameterShape { get; set; }
        public object value { get; set; }

    }
    public class DBParameterOP
    {
        private readonly string _connectionString;
        public DBParameterOP(string connectionString)
        {
            _connectionString = connectionString;
        }
        public void AddParameterToDatabase(DBParameterShape p)
        {
            using (var connection = new SQLiteConnection(_connectionString))
            {
                connection.Open();
                using (var command = new SQLiteCommand($"INSERT INTO DBParameter (dataName, dataAddress,dataType,dataLength,unit,remark) VALUES ('{p.dataName}','{p.dataAddress}','{p.dataType}',{p.dataLength},'{p.unit}','{p.remark}')", connection))
                {
                    command.ExecuteNonQuery();
                }
            }
        }
        public void UpdateParameterInDatabase(DBParameterShape p)
        {
            using (var connection = new SQLiteConnection(_connectionString))
            {
                connection.Open();
                using (var command = new SQLiteCommand($"UPDATE DBParameter SET dataName = '{p.dataName}', dataAddress = '{p.dataAddress}',dataType='{p.dataType}',dataLength={p.dataLength},unit='{p.unit}',remark='{p.remark}' WHERE id = {p.id}", connection))
                {
                    command.ExecuteNonQuery();
                }
            }
        }
        public void DeleteParameterFromDatabase(DBParameterShape p)
        {
            using (var connection = new SQLiteConnection(_connectionString))
            {
                connection.Open();
                using (var command = new SQLiteCommand($"DELETE FROM DBParameter WHERE id = {p.id}", connection))
                {
                    command.ExecuteNonQuery();
                }
            }
        }
    }
}
